﻿using DE_MVC_WyFy.Models;
using DE_MVC_WyFy.Models.Enities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DE_MVC_WyFy.Controllers
{
    public class ShoppingCartController : Controller
    {
        //
        // GET: /ShoppingCart/

		public ActionResult Index()
		{
			var cart = ShoppingCart.GetCart(this.HttpContext);

			// Set up our ViewModel
			var viewModel = new ShoppingCartView
			{
				CartItems = cart.GetCartItems(),
				CartTotal = cart.GetTotal()
			};

			// Return the view
			return View(viewModel);
		}

		//
		// GET: /Store/AddToCart/5

		public ActionResult AddToCart(String id)
		{

			// Retrieve the Product from the database
			var addedProd= Product.Objects.GetById(id);

			// Add it to the shopping cart
			var cart = ShoppingCart.GetCart(this.HttpContext);

			cart.AddToCart(addedProd);

			// Go back to the main store page for more shopping
			return RedirectToAction("Index");
		}
		[HttpPost]
		public ActionResult RemoveFromCart(int id)
		{
			// Remove the item from the cart
			var cart = ShoppingCart.GetCart(this.HttpContext);

			// Get the name of the album to display confirmation
			string prodName = Cart.Objects.GetById(id).ProdName;

			// Remove from cart
			int itemCount = cart.RemoveFromCart(id);

			// Display the confirmation message
			var results = new ShoppingCartRemoveView
			{
				Message = Server.HtmlEncode(prodName) +
					" has been removed from your shopping cart.",
				CartTotal = cart.GetTotal(),
				CartCount = cart.GetCount(),
				ItemCount = itemCount,
				DeleteID = id
			};

			return Json(results);
		}

    }
}
